package com.hoo.servlet;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import com.hoo.common.constant.Constant;
import com.hoo.entity.order.Ticket;
import com.hoo.service.order.TicketOrderService;

@Component("orderQuerryByTimeJob")
public class OrderQuerryByTimeJob {
/*	@Autowired
	InvoiceMachineService invoiceMachineService1;*/
	
	//volatile List<InvoiceMachine> list1;

    /**
     * 获取当前可在使用的票务类型
     */
    @Scheduled(fixedDelay=200000,initialDelay=6000)   //每10秒执行一次,6秒后启动  
    public void checkOnUseTicket() {
    	WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
    	TicketOrderService ticketOrderService= (TicketOrderService)wac.getBean("ticketOrderService");
    	try {
    		Ticket ticket = ticketOrderService.getTicketOnUse();
    		if(null != ticket){
        		Constant.ticketNo=ticket.getTicketNo();	
        	}
		} catch (Exception e) {
			
		}
    }
    
    
	
    /**
     * 监控取票机的状态，并作出相应应修改
     */
    /*@Scheduled(fixedDelay=20000,initialDelay=6000000)   //每10秒执行一次,6秒后启动  
    public void updateCardStock() {
    	List<InvoiceMachine> list = this.invoiceMachineService1.getAllInvoiceMachine(); 
    	final OrderQuerryByTimeJob querryByTimeJob=new OrderQuerryByTimeJob();
    	list1=new ArrayList<InvoiceMachine>();
    	for (final InvoiceMachine invoiceMachine:list) {
    		new Thread(new Runnable() {  
    			WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
    			InvoiceMachineService invoiceMachineService = (InvoiceMachineService)wac.getBean("invoiceMachineService");
				@Override  
                public void run() {  
					boolean result = querryByTimeJob.test(invoiceMachine);
            		if (!result && invoiceMachine.getConnectStatus()!=2) {
                		invoiceMachine.setConnectStatus(2);
                		this.invoiceMachineService.updateInvoiceMachineByIp(invoiceMachine);
        	        }else if(result && invoiceMachine.getConnectStatus()!=1) {
        	        	invoiceMachine.setConnectStatus(1);
        	        	this.invoiceMachineService.updateInvoiceMachineByIp(invoiceMachine);
        	        }
                } 
            }).start();  
    	}
    }
    
    public boolean test(InvoiceMachine invoiceMachine){
    	InetAddress address=null;
    	boolean result=false;
    	try {
 	        address = InetAddress.getByName(invoiceMachine.getIp());
 	        result = address.isReachable(2000);
    	 }catch(Exception e){
    		 e.printStackTrace();
    	 }
    	return result;
    }*/
}
